#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"

void QueueInit(Q* p)
{
	assert(p);
	p->head = p->tail = NULL;
}

void QueuePush(Q* p, QueueDataType x)
{
	assert(p);
	QN* newnode = (QN*)malloc(sizeof(QN));
	newnode->next = NULL;
	newnode->Data = x;
	if (newnode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	if (p->head == NULL)
	{
		p->head = p->tail = newnode;
	}
	else
	{
		p->tail->next = newnode;
		p->tail = newnode;
	}
}

void QueuePrint(Q* p)
{
	assert(p);
	QN* cur = p->head;
	while (cur != NULL)
	{
		printf("%d ",cur->Data);
		cur = cur->next;
	}
}

void QueuePop(Q* p)
{
	assert(p);
	assert(p->tail);
	if (p->head->next = NULL)
	{
		free(p->head);
		p->head = p->tail = NULL;
	}
	else
	{
		QN* next = p->head->next;
		free(p->head);
		p->head = next;
	}
}

bool QueueEmpty(Q* p)
{
	assert(p);
	return p->head = NULL;
}

QueueDataType QueueFront(Q* p)
{
	assert(p);
	assert(p->head == NULL);
	return p->head->Data;
}

QueueDataType QueueBack(Q* p)
{
	assert(p);
	assert(p->head == NULL);
	return p->tail->Data;
}

int QueueSize(Q* p)
{
	assert(p);
	int size = 0;
	QN* cur = p->head;
	while (cur != NULL)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

void QueueDestory(Q* p)
{
	assert(p);
	QN* cur = p->head;
	while (cur != NULL)
	{
		QN* next = cur->next;
		free(cur);
		cur = next;
	}
	p->head = p->tail = NULL;
}